﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace DBLibrary
{
    /// <summary>
    /// PicasaLiteDLL is a DLL project offering Database operations
    /// </summary>
    public class PicasaLiteDLL
    {
        protected SqlConnection connection;

        /// <summary>
        /// Default constructor
        /// </summary>
        public PicasaLiteDLL()
        {
            // Initialize SqlConnection
            //
            string strConnection = "Server=JORIS;Database=PicasaLiteDB;Integrated Security=true;";
            connection = new SqlConnection(strConnection);
        }

        /// <summary>
        /// Get the next id of the table
        /// </summary>
        /// <param name="table">Table's name</param>
        /// <returns>Return the next ID</returns>
        public int getNextID(String table)
        {
            try
            {
                // Build the SQL query
                //
                SqlCommand selectMaxID = new SqlCommand("SELECT MAX(id) FROM " + table, connection);

                // Execute Query
                //
                Object nextID = selectMaxID.ExecuteScalar();
                if (nextID == DBNull.Value) return 1;
                else return (int)nextID + 1;
            }
            catch (SqlException e)
            {
                Console.WriteLine("An SQL error occured : " + e.Message);
            }
            catch (InvalidOperationException e)
            {
                Console.WriteLine("An error occured while openning the connection : " + e.Message);
            }

            return -1;
        }
    }
}
